<?php

namespace app\controller\api\v1\recruit;

use app\Request;
use app\model\v1\recruit\RecruitPersonageAuthentication;
use app\services\product\category\StoreProductCategoryServices;
use app\validate\api\recruit\authentValidate;


/**
 * 求职招聘
 * Class RecruitCollection
 * @package app\controller\api\recruit
 */
class RecruitPersonAuthent
{
    protected $services;
    protected $model;
    protected $userinfo = [];

    public function __construct(StoreProductCategoryServices $services)
    {
        $this->services = $services;
        $this->model = new RecruitPersonageAuthentication();
    }


    /**
     * 个人认证创建
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function authentication_create(Request $request)
    {
        $data = $request->postMore([
            ['user_name', ''],
            ['Identity_id', ''],
            ['identity_positive', ''],
            ['identity_reverse', ''],
        ]);
        validate(authentValidate::class)->check($data);
        $user_name = trim($data['user_name']);
        ////if(!preg_match("/^[\p{L}\p{Han}\s]+(?:\s?[\p{L}\p{Han}\s])*$/u", $user_name)) return app('json')->fail('姓名只能包含中文、英文和空格！');
        if(!preg_match("/^[\x{4e00}-\x{9fa5}\s]+$/u", $user_name)) return app('json')->fail('姓名只能是中文！');


        $this->param_unset($data);
        $res = $this->model->where(['u_id'=>$request->uid()])->whereIn("status", [0, 1])->whereNull("delete_at")->find();
        if ($res){
            return app('json')->fail('无需添加个人认证');
        }
        $res = $this->model->where(['Identity_id'=>$data['Identity_id']])->whereIn("status", [0, 1])->whereNull("delete_at")->find();
        if ($res){
            return app('json')->fail('身份证号重复！');
        }

        $data['u_id'] = $request->uid();
        $this->model->create($data);
        return app('json')->success('个人认证添加成功.请等待审核');
    }


    public function param_unset(&$params)
    {
        unset($params['token']);
        unset($params['sign']);
        unset($params['app_id']);
        unset($params['sign_key']);
        unset($params['version']);
    }


    /**
     * 编辑个人认证
     * @param $id
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function authentication_update(Request $request, $id)
    {
        $data = $request->postMore([
            ['user_name', ''],
            ['Identity_id', ''],
            ['identity_positive', ''],
            ['identity_reverse', ''],
        ]);
        validate(authentValidate::class)->check($data);

        $this->param_unset($data);
        $seeker = $this->model->where("id", $id)->find();
        if ($seeker) {
            $data['u_id'] = $request->uid();
            $seeker->save($data);
            return app('json')->success('个人认证更新成功');
        }
        return app('json')->fail('个人认证不存在');
    }



    /**
     * 获取个人认证详情
     * @return mixed
     */
    public function authentication_detail($id)
    {
        $where['u_id'] = $id;
        $seeker = $this->model->where($where)->order("id","desc")->limit(1)->find();
        if ($seeker) {
            return app('json')->success($seeker->toArray());
        }
        return app('json')->fail('当前id数据不存在');
    }


}